<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Department
 *
 *
 * @package     CodeIgniter
 * @subpackage  Rest Server
 * @category    Controller
 * @author      lcdung
 * @link        http://bungtay.com/hrm3/
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class Paygrade extends REST_Controller{
    
    function __construct(){
        parent::__construct();
        //$this->load->model('user','',TRUE);
        $this->load->model(array('paygrade_model','user_model'));

    }
    
    public function paygradeitem_get()
    {
        $token = $this->get('token');
        $token_secret = $this->get('token_secret');

        $this->oauth($token);

        $data = $this->paygrade_model->getpaygrade(array('idpaygrade'=>2));
        if($data){
            $this->response(array('result'=>$data));
        }else{
            $this->response(array('error' => 'Could not be found'), 404);
        }
    }

    public function updatepaygrade_post(){

            $params = json_decode(file_get_contents('php://input'), TRUE);

            $token = $this->get('token');

            

            $this->oauth($token);
            
            $id = 2;
            
            $salary = $params['salary'];
            
            $param['salary']        = round($salary, 2);
            $param["dateupdated"]   = time();
            
            $condition = array('idpaygrade'=>$id);
            
            if($this->paygrade_model->updatepaygrade($param,$condition)){
                $this->response(array('result'=>'true'), 200);    
            }else{
                $this->response(array('result'=>'false'), 200);    
            }

    }

    function oauth($token,$token_secret=null){
        //token=ab3cd9j4ks73hf7g&token_secret=xyz4992k83j47x0b
        $oauth = $this->user_model->auth_token($token);
        if($oauth){
            if(time()<$oauth['date_created']){
                    $cnttime = time()+10800;
                        
                    $data = array('date_created'=>$cnttime);
                    $cont = array('id' => $oauth['id']);
                    $xs = $this->user_model->updateKey($data,$cont);
                    //$this->response($xs, 200);
            }else{
                $this->response(array('error' => 'Wrong token'), 401);
            }
        }else{
            $this->response(array('error' => 'Wrong token'), 401);
        }
    }
        
}

?>